package com.jltx.xq.simplewatch.news.ui;

import android.app.Activity;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;

import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.App;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;
import com.jltx.xq.simplewatch.base.presenter.BasePresenter;
import com.jltx.xq.simplewatch.db.SimpleWatchContract;
import com.jltx.xq.simplewatch.news.api.TopicNewsApiService;
import com.jltx.xq.simplewatch.news.model.bean.TopicNewsEntity;
import com.jltx.xq.simplewatch.video.model.impl.TopicNewsModelImpl;
import com.jltx.xq.simplewatch.video.model.impl.TopicNewsNetModelImpl;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jltx.utils.XqLog;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/20.
 */

public class TopicNBANewsFragment extends BaseSupportFragment{

    @BindView(R.id.addbtn)
    Button addBtn;
    @BindView(R.id.querybtn)
    Button queryBtn;
    @BindView(R.id.delbtn)
    Button delBtn;
    @BindView(R.id.updatebtn)
    Button updateBtn;

    public static TopicNBANewsFragment newInstance() {
        TopicNBANewsFragment fragment = new TopicNBANewsFragment();
        return fragment;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_topic_nba_news_act;
    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @Override
    public void initView(View rootView, Bundle savedInstanceState) {
        super.initView(rootView, savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        App.getApp().getContentResolver().registerContentObserver(SimpleWatchContract.CONTENT_NEWS_RECORD_URI, true, contentObserver);
    }

    private Handler handler = new Handler(Looper.getMainLooper());
    private ContentObserver contentObserver = new ContentObserver(handler) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            super.onChange(selfChange, uri);
            XqLog.d("CustomProvider","数据库变更"+(uri != null ? uri : "null"));
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

        }
    };

    @Override
    public void onDetach() {
        super.onDetach();
        App.getApp().getContentResolver().unregisterContentObserver(contentObserver);
    }


    @OnClick({R.id.addbtn,R.id.querybtn,R.id.delbtn,R.id.updatebtn})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.addbtn:
//                initTopNews();
//                int lastPage = NewsRecordDBHelper.getLastNewsRecordNoNull(getString(R.string.wechat_code)).getPage()+1;
//                SimpleWatchDBDemo.insert(getString(R.string.wechat_code),lastPage,(int)System.currentTimeMillis());
//                SimpleWatchDBDemo.queryNewsStateRx(getString(R.string.wechat_code),"http://www.baidu.com");
                getTopicNewsJson();
                break;
            case R.id.querybtn:
//                getDownNews();
//                SimpleWatchDBDemo.queryMaxItemRx(getString(R.string.wechat_code));
                getTopicNewsNetModel();
                break;
            case R.id.delbtn:
                getUpNews();
//                SimpleWatchDBDemo.insertNewsStateRx(getString(R.string.wechat_code));
                break;
            case R.id.updatebtn:
//                int lastPage = NewsRecordDBHelper.getLastNewsRecordNoNull(getString(R.string.wechat_code)).getPage()-1;
//                SimpleWatchDBDemo.queryItemRx(getString(R.string.wechat_code),lastPage);
                break;
        }
    }

    private TopicNewsModelImpl topicNewsModel = new TopicNewsModelImpl();

    public void initTopNews(){
        topicNewsModel.getTopicNews("video",true).observeOn(AndroidSchedulers.mainThread()).subscribe(getObserver());
    }

    public void getDownNews(){
        topicNewsModel.getTopicNews("video",true).observeOn(AndroidSchedulers.mainThread()).subscribe(getObserver());
    }

    public void getUpNews(){
        topicNewsModel.getTopicNews("video",false).observeOn(AndroidSchedulers.mainThread()).subscribe(getObserver());
    }

    private Observer<TopicNewsEntity> getObserver(){
        return new Observer<TopicNewsEntity>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(TopicNewsEntity topicNewsEntity) {
                XqLog.d(TopicNewsModelImpl.TAG, "WeChatPresenterImpl.onNext"+Thread.currentThread());
                XqLog.d(TopicNewsModelImpl.TAG, "topicNewsEntity=>"+topicNewsEntity.toString());
            }

            @Override
            public void onError(Throwable e) {
                XqLog.d(TopicNewsModelImpl.TAG, "onError出错" + e.getMessage());
            }

            @Override
            public void onComplete() {
                XqLog.d(TopicNewsModelImpl.TAG, "onComplete");
            }
        };
    }

    public void getTopicNewsJson() {
        TopicNewsApiService.getInstance().getTopicNewsApi().getTopicNewsJson("video", System.currentTimeMillis() / 1000, System.currentTimeMillis() / 1000)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        XqLog.d(TopicNewsModelImpl.TAG, "onSubscribe");
                    }

                    @Override
                    public void onNext(String s) {
                        XqLog.d(TopicNewsModelImpl.TAG, "onNext=>" + s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        XqLog.d(TopicNewsModelImpl.TAG, "onError出错" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        XqLog.d(TopicNewsModelImpl.TAG, "onComplete");
                    }
                });
    }



    public void getTopicNewsNetModel(){
         TopicNewsNetModelImpl topicNewsNetModel = new TopicNewsNetModelImpl();
        topicNewsNetModel.getTopicNews("",true)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<TopicNewsEntity>() {
            @Override
            public void onSubscribe(Disposable d) {
                XqLog.d(TopicNewsModelImpl.TAG, "onSubscribe");
            }

            @Override
            public void onNext(TopicNewsEntity topicNewsEntity) {
                XqLog.d(TopicNewsModelImpl.TAG, "onNext=>" + topicNewsEntity.toString());
            }

            @Override
            public void onError(Throwable e) {
                XqLog.d(TopicNewsModelImpl.TAG, "onError出错" + e.getMessage());
            }

            @Override
            public void onComplete() {
                XqLog.d(TopicNewsModelImpl.TAG, "onComplete");
            }
        });

    }
}
